package com.administrator.cloudapplication.activitys;

import android.view.View;
import android.widget.Toast;

import com.administrator.cloudapplication.R;
import com.administrator.cloudapplication.adapter.MediaSearchMoreAdapter;
import com.administrator.cloudapplication.base.BaseActivity;
import com.administrator.cloudapplication.callback.ReqCallBack;
import com.administrator.cloudapplication.constant.Urls;
import com.administrator.cloudapplication.models.EbookSearchMoreModel;
import com.administrator.cloudapplication.models.MediaSearchMoreModel;
import com.administrator.cloudapplication.tools.GsonUtils;
import com.administrator.cloudapplication.tools.HttpUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/4/19.
 */

public class SearchMoreActivity extends BaseActivity implements ReqCallBack ,PullToRefreshBase.OnRefreshListener2{
    String cate;
    String title_name;
    int type;
    int curpage=1;
    private PullToRefreshGridView mGridView;
    List<MediaSearchMoreModel.DataBean> mDataBean = new ArrayList<>();
    List<EbookSearchMoreModel.DataBean> mEbookDataBean = new ArrayList<>();
    MediaSearchMoreAdapter mVideoGridViewAdapter;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_searchmore);
        mGridView = (PullToRefreshGridView) findViewById(R.id.gv_searchformore);
        cate = getIntent().getStringExtra("cate");
        type = Integer.parseInt(getIntent().getStringExtra("type"));
        title_name=getIntent().getStringExtra("title_name");
        mGridView.setOnRefreshListener(this);
        mGridView.setMode(PullToRefreshBase.Mode.BOTH);
        initTitleView();
        tvTitle.setText(title_name);
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        requestWeb(curpage);
    }

    private void requestWeb(int curpage) {
        switch (type) {
            case 0:
                HashMap<String, String> param = new HashMap();
                param.put("cate", cate);
                param.put("curpage", curpage+"");
                HttpUtils.getInstance(this).requestPostByAsyn2(Urls.VIDEO_MORE, param, this, 0);
                break;
            case 1:
                param = new HashMap();
                param.put("cate", cate);
                param.put("curpage", curpage+"");
                HttpUtils.getInstance(this).requestPostByAsyn2(Urls.AUDIO_MORE, param, this, 0);
                break;
            case 2:
                param = new HashMap();
                param.put("cate", cate);
                param.put("curpage", curpage+"");
                HttpUtils.getInstance(this).requestPostByAsyn2(Urls.BOOK_MORE, param, this, 0);
                break;
        }

    }

    @Override
    protected void onLeftClick() {

    }

    @Override
    protected void onRightClick() {

    }

    @Override
    public void onReqSuccess(String result, int flag) {
        stopLoading();
        if (mGridView.isRefreshing()) {
            mGridView.onRefreshComplete();
        }

        switch (type) {
            case 0:
            case 1:
                mDataBean.clear();
                MediaSearchMoreModel mMediaSearchMoreModel = GsonUtils.json2Bean(result, MediaSearchMoreModel.class);
                mDataBean.addAll(mMediaSearchMoreModel.getData());
                mVideoGridViewAdapter = new MediaSearchMoreAdapter(this, mDataBean, null, 0);
             // mGridView.setNumColumns(2);
                break;
            case 2:
                mEbookDataBean.clear();
                EbookSearchMoreModel mEbookSearchMoreModel = GsonUtils.json2Bean(result, EbookSearchMoreModel.class);
                mEbookDataBean.addAll(mEbookSearchMoreModel.getData());
                mVideoGridViewAdapter = new MediaSearchMoreAdapter(this, null, mEbookDataBean, 1);
               //mGridView.setNumColumns(3);
                break;
        }
        mGridView.setAdapter(mVideoGridViewAdapter);
    }

    @Override
    public void onReqFailed(String errorMsg, int flag) {
        Toast.makeText(this,"请检查您的网络",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        requestWeb(curpage);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        requestWeb(curpage);
    }
}
